/*
 * SessionManager.h
 *
 *  Created on: Mar 9, 2017
 *      Author: ectrs
 */

#ifndef SRC_SESSIONMANAGER_H_
#define SRC_SESSIONMANAGER_H_

#include <Poco/Dynamic/Var.h>
#include <Poco/UUID.h>
#include <Poco/Util/Timer.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/DatagramSocket.h>
#include <Poco/Net/IPAddress.h>
#include <vector>
#include <map>

using namespace Poco;
using namespace Poco::Util;
using namespace Poco::Dynamic;
using namespace Poco::Net;

#include "MySession.h"

class SessionManager
{
public:
	enum ENUM_SOCKET_MODE {
		SOCKET_MODE_SERVER,
		SOCKET_MODE_CLIENT
	};
	static bool initialize(ENUM_SOCKET_MODE mode);
	static int receiveFrom(char* buffer, int length, SocketAddress& address);
	static int receiveBytes(char* buffer, int length);
	static int sendTo(const char* buffer, int length, SocketAddress& address);
	static int sendBytes(const char* buffer, int length);

	static void doLogin(const Var params, std::string& strResp);
	static void doLogout(const Var params, std::string& strResp);
	static void doQuery(const Var params, std::string& strResp);

	static void timeout(MySession& session);

private:
	static std::vector<MySession> _vecSessions;

	static UUID getFreeUUID();
	static bool findSessionWithSid(std::string& sid, MySession& session);

	static bool registSession(MySession& session);
	static bool unregistSession(MySession& session);

	static DatagramSocket _socket;
	static Timer _timer;
};

#endif /* SRC_SESSIONMANAGER_H_ */
